﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class CourseEdit : System.Web.UI.Page
    {
        protected string Cno;
        protected string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Cno"] == null)
            {
                Response.Redirect("CourseManage.aspx?item=1");         // 返回到主页面
            }
            else
            {
                Cno = Request.QueryString["Cno"].ToString().Replace("'", "''");
                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        protected void BindData()
        {
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("SELECT * FROM course WHERE cno = '{0}'", Cno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataReader dr = myCommand.ExecuteReader();
            if (dr.Read())
            {
                txtCno.Text = dr["cno"].ToString();
                txtCname.Text = dr["cname"].ToString();
                txtLecture.Text = dr["lecture"].ToString();
                txtSemester.Text = dr["semester"].ToString();
                txtCredit.Text = dr["credit"].ToString();
            }
            else
            {
                Response.Redirect("CourseManage.aspx?item=1");         // 返回到主页面
            }
        }


        protected void btnReset_Click(object sender, EventArgs e)
        {
            BindData();
        }


        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("CourseManage.aspx?item=1");
        }

        protected void btnOK_Click1(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string Cno = txtCno.Text.Trim().Replace("'", "''");
                string Cname = txtCname.Text.Trim().Replace("'", "''");
                string Lecture = txtLecture.Text.Trim().Replace("'", "''");
                string Semester = txtSemester.Text.Trim().Replace("'", "''");
                string Credit = txtCredit.Text.Trim().Replace("'", "''");


                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                string strSql = string.Format("UPDATE course SET cname='{1}', lecture='{2}', semester='{3}', credit='{4}' WHERE cno='{0}'", Cno, Cname, Lecture, Semester, Credit);
                SqlCommand myCommand = new SqlCommand(strSql, conn);
                myCommand.ExecuteNonQuery();                            // 执行命令
                Response.Redirect("CourseManage.aspx?item=1");         // 返回到主页面
            }
        }
    }
}